package com.spzx.product.api.factory;

import com.spzx.common.core.domain.R;
import com.spzx.product.api.RemoteCategoryService;
import com.spzx.product.api.domain.vo.CategoryVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
//@Slf4j
public class RemoteCategoryFallbackFactory implements FallbackFactory<RemoteCategoryService> {

    public static final Logger log = LoggerFactory.getLogger(RemoteCategoryFallbackFactory.class);

    @Override
    public RemoteCategoryService create(Throwable cause) {
        log.error("商品分类服务调用降级："+cause.getMessage());
        return new RemoteCategoryService() {
            @Override
            public R<List<CategoryVo>> getOneCategory(String source) { //远程调用出问题（超时,异常）了，就走降级方法（返回兜底数据）。
                //return R.fail(new ArrayList<CategoryVo>());
                return R.fail("获取全部一级分类失败:" + cause.getMessage()); //编译没有报错，运行会报错吗？ 不会。泛型擦除
            }

            @Override
            public R<List<CategoryVo>> tree(String source) {
                return R.fail("获取分类树结构失败:" + cause.getMessage());
            }
        };
    }
}
